package edu.gdkm.controller;

import edu.gdkm.sql.pojo.Userinfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller

public class UserController {
    @RequestMapping("/logincheck")
    public String logincheck(Model model, String username, String password, HttpSession session) {

        if ("admin".equals(username) && "123456".equals(password)) {
            model.addAttribute("name",username);            //model > request > session > application
            session.setAttribute("username",username);
            return "index";
        }else{
            return "redirect:/login.jsp";
        }

    }
    @RequestMapping("/register")
    public String register(String username, String password) {
        System.out.println(username+"  "+password);
        return "index";
    }
    @RequestMapping("/updateuserinfo")
    public String updateuserinfo(Userinfo userinfo,Model model){
        model.addAttribute("userinfo",userinfo);
        return "userinfo";
    }
    @RequestMapping("/logincheckByJson")
    @ResponseBody
    public Userinfo logincheckByJson(@RequestBody Userinfo userinfo){
        System.out.println(userinfo);
        return userinfo;
    }
    @RequestMapping("/show")
    public void show(Model model){
        Userinfo userinfo = new Userinfo();
        userinfo.setName("李清");
        userinfo.setAddress("广东省清远市清城区广东科贸职业学院");
        userinfo.setPhone("13800138000");
        model.addAttribute("userinfo",userinfo);
        model.addAttribute("title","清远市技能大赛");
    }
    @RequestMapping("/show2")
    public String show2(Model model){
        Userinfo userinfo = new Userinfo();
        userinfo.setName("李四");
        userinfo.setAddress("广东省清远市清城区广东科贸职业学院");
        userinfo.setPhone("13800138000");
        model.addAttribute("userinfo",userinfo);
        return "show";
    }
    @RequestMapping("/show3")
    public String show3(Model model){
        Userinfo userinfo = new Userinfo();
        userinfo.setName("李四");
        userinfo.setAddress("广东省清远市清城区广东科贸职业学院");
        userinfo.setPhone("13800138000");
        model.addAttribute("userinfo",userinfo);
        return "forward:\\WEB-INF\\jsp\\show.jsp";
    }
    @RequestMapping("/show4")
    public String show4(Model model){
        Userinfo userinfo = new Userinfo();
        userinfo.setName("李四");
        userinfo.setAddress("广东省清远市清城区广东科贸职业学院");
        userinfo.setPhone("13800138000");
        model.addAttribute("userinfo",userinfo);
        return "redirect:http://www.baidu.com";
    }
}
